/**
 * 考试信息管理初始化
 */
var SeniorExaminee = {
    id: "SeniorExamineeTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SeniorExaminee.initColumn = function () {
    return [
            {title: '主键ID', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '状态', field: 'writeStatus', visible: false, align: 'center', valign: 'middle'},
            {title: '准考证号', field: 'examCardNumber', visible: true, align: 'center', valign: 'middle'},
            {title: '考生姓名', field: 'studentName', visible: true, align: 'center', valign: 'middle'},
            {title: '所属学校', field: 'schoolName', visible: true, align: 'center', valign: 'middle'},
            {title: '所属班级', field: 'className', visible: true, align: 'center', valign: 'middle'},
            {title: '性别', field: 'gender', visible: true, align: 'center', valign: 'middle'},
            {title: '身份证号码', field: 'idCard', visible: true, align: 'center', valign: 'middle'},
            {title: '出生年月', field: 'birthday', visible: true, align: 'center', valign: 'middle',formatter: function (value, row, index) {
                return (value!=null?value.substring(0,10):"");
            }},
            {title: '民族', field: 'nation', visible: true, align: 'center', valign: 'middle'},
            {title: '联系电话1', field: 'phone1', visible: true, align: 'center', valign: 'middle'},
            {title: '联系电话2', field: 'phone2', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
SeniorExaminee.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SeniorExaminee.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加考试信息
 */
SeniorExaminee.openAddExaminee = function () {
    var index = layer.open({
        type: 2,
        title: '添加考生信息',
        area: ['60%', '60%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/seniorExaminee/edit'
    });
    this.layerIndex = index;
};

/**
 * 打开查看考试信息详情
 */
SeniorExaminee.openChangeExaminee = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改考生信息',
            area: ['60%', '60%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/seniorExaminee/edit?id=' + SeniorExaminee.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除考试信息
 */
SeniorExaminee.delSchool = function () {
    if (this.check()) {
    	if(SeniorExaminee.seItem.writeStatus==1){
    		 Feng.error("该考生已经填报志愿,不能删除!");
    	}else{
    		var operation = function(){
    	        var ajax = new $ax(Feng.ctxPath + "/seniorExaminee/remove", function (data) {
    	            Feng.success("删除成功!");
    	            SeniorExaminee.table.refresh();
    	        }, function (data) {
    	            Feng.error("删除失败!" + data.responseJSON.message + "!");
    	        });
    	        ajax.set("examineeId",SeniorExaminee.seItem.id);
    	        ajax.start();
        	 };
        	 Feng.confirm("是否删除考生" + SeniorExaminee.seItem.studentName + "？",operation);
    	}
    }
};

/**
 * 考生模板下载
 */
SeniorExaminee.templateDown = function () {
	var operation = function(){
		location.href=Feng.ctxPath + "/static/file/考生信息模板.xlsx";
	 };
	 Feng.confirm("确定下载考生导入模板？",operation);
};
/**
 * 考生导入
 */
SeniorExaminee.importExaminee = function () {
	 //定义上传文件对象
    var webUploaderObj = new $WebUpload("importExaminee");
    //设置允许上传的文件类型（在web-upload-object中定义了类型）
    webUploaderObj.setAcceptFileType("excel");
    //设置文件上传地址
    webUploaderObj.setUploadUrl(Feng.ctxPath + "/seniorExaminee/importExaminee");
    //获取webUploader对象，调用create方法，手动绑定事件
    var webUploader = webUploaderObj.create();
    //绑定事件
    webUploader.on('uploadSuccess', function (file, response) {
        Feng.infoDetail("导入结果", response.message.split("\n"), '500px', '500px');
        SeniorExaminee.table.refresh();
    });
    // 文件上传失败，显示上传出错。
    webUploader.on('uploadError', function (file, response) {
        Feng.error("上传失败");
    });
};

SeniorExaminee.resetSearch = function () {
    $("#seniorSchoolId").val("").trigger("change");
    $("#seniorClassId").val("").trigger("change");
    $("#examCardNumber").val("");
    $("#studentName").val("");
    SeniorExaminee.search();
}

/**
 * 查询考试信息列表
 */
SeniorExaminee.search = function () {
    var queryData = {};
    queryData['examCardNumber'] = $("#examCardNumber").val();
    queryData['studentName'] = $("#studentName").val();
    queryData['seniorSchoolId'] = $("#seniorSchoolId").select2("val");
    queryData['seniorClassId'] = $("#seniorClassId").select2("val");
    SeniorExaminee.table.refresh({query: queryData});
};

/**
 * 获取shoolId
 */
SeniorExaminee.getSchoolId=function(schoolSelectId){
    return {
        schoolId:$("#"+schoolSelectId).select2("val")
    }
}

$(function () {
    var defaultColumns = SeniorExaminee.initColumn();
    var table = new BSTable(SeniorExaminee.id, "/seniorExaminee/list", defaultColumns);
    SeniorExaminee.table = table.init();
    SeniorExaminee.importExaminee();
});
